package com.air.admin.service.imp;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.admin.dao.AdminFunctionItemDao;
import com.air.admin.mo.AdminFunctionItem;
import com.air.admin.mo.AdminPrivilege;
import com.air.admin.service.IAdminFunctionItemService;
import com.air.common.service.imp.BaseServiceImp;
import com.air.common.util.BasicDBDictionary;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;

@Service("adminFunctionItemService")
public class AdminFunctionItemServiceImp extends BaseServiceImp implements
		IAdminFunctionItemService, InitializingBean {
	
	@Autowired
	AdminFunctionItemDao adminFunctionItemMapper;
	
	
	
	@Override
	public void afterPropertiesSet() throws Exception {
		this.baseDao = adminFunctionItemMapper;
	}
	
	@Override
	public List<AdminFunctionItem> getAvailableFunItems(
			List<AdminPrivilege> availablePrivileges) {
		
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("status", BasicDBDictionary._common_status_available, QueryExpression.EQUALS);
		List<AdminFunctionItem> allFunItems = adminFunctionItemMapper.queryByCondition(condition);
		List<AdminFunctionItem> availableFunItems = new ArrayList<AdminFunctionItem>();
		for(AdminFunctionItem funItem:allFunItems){
			if (isAvailableFunItem(availablePrivileges,funItem)){
				availableFunItems.add(funItem);
			}
		}
		
		return availableFunItems;
	}

	private boolean isAvailableFunItem(List<AdminPrivilege> availablePrivileges,AdminFunctionItem funItem){
		
		boolean isAvailable = false;
		
		for(AdminPrivilege privilege:availablePrivileges){
			System.out.println(privilege.getCode()+":"+funItem.getPrivilegeCode());
			if (privilege.getCode().equalsIgnoreCase(funItem.getPrivilegeCode())){
				isAvailable = true;
				break;
			}
		}
		
		return isAvailable;
	}

}
